package com.lalifa.googledate.util

import android.app.Activity
import com.drake.net.utils.scopeNet
import com.lalifa.googledate.api.UserBean
import com.lalifa.googledate.ui.MApplication
import com.lalifa.utils.ObjToSP
import com.lalifa.utils.SPUtil

/**
 * @author: BaiCQ
 * @ClassName: UserManager
 * @Description: 信息的缓存辅助类
 */
class UserManager private constructor() : ObjToSP<UserBean?>("SP_USER") {
    private var current: UserBean? = null

    /**
     * 保存当前用户信息
     *
     * @param user User
     */
    private fun saveUser(user: UserBean?) {
        if (null != user) {
            current = user
            super.saveEntity(TAG, current)
        }
    }

    /**
     * 获取最新用户的信息
     */
    private val user: UserBean?
        get() {
            return current
        }


    private fun clear() {
        current = null
        super.deleteFast(TAG)
    }

    companion object {
        private val _manager = UserManager()
        fun get(): UserBean? {
            return _manager.user
        }

        fun save(user: UserBean) {
            _manager.saveUser(user)
        }

        fun logout(context: Activity) {
            scopeNet {
            //    val loginOutApi = loginOutApi()
             //   if(loginOutApi.code==1){
                    MApplication.get().mGoogle!!.signOut()
                        .addOnCompleteListener(context) {
                            _manager.clear()
                            SPUtil.clear()
                        }
             //   }
            }
        }
    }
}